package com.zbkj.front.controller.merchant;

import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.FundsFlowRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.merchant.MerchantClosingApplyRequest;
import com.zbkj.common.request.merchant.MerchantClosingSearchRequest;
import com.zbkj.common.response.*;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.FinanceService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 商户端财务控制器
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/merchant/finance")
@Api(tags = "商户端财务控制器")
public class MerchantFinanceController {

    @Autowired
    private FinanceService financeService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "资金流水分页列表")
    @RequestMapping(value = "/funds/flow", method = RequestMethod.GET)
    public CommonResult<CommonPage<FundsFlowResponse>> getFundsFlow(@Validated FundsFlowRequest request,
                                                                    @Validated PageParamRequest pageParamRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(CommonPage.restPage(financeService.getMerchantFundsFlow(request, pageParamRequest, systemAdmin)));
    }

    @ApiOperation(value = "获取结算申请基础信息")
    @RequestMapping(value = "/closing/base/info", method = RequestMethod.GET)
    public CommonResult<MerchantClosingBaseInfoResponse> getClosingBaseInfo() {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(financeService.getClosingBaseInfo(systemAdmin));
    }

    @ApiOperation(value = "结算申请")
    @RequestMapping(value = "/closing/apply", method = RequestMethod.POST)
    public CommonResult<String> closingApply(@RequestBody @Validated MerchantClosingApplyRequest request) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        if (financeService.merchantClosingApply(request, systemAdmin)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "结算记录分页列表")
    @RequestMapping(value = "/closing/record/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MerchantClosingPageResponse>> getMerchantClosingPageList(@Validated MerchantClosingSearchRequest request,
                                                                                            @Validated PageParamRequest pageParamRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(CommonPage.restPage(financeService.getMerchantClosingRecordPageList(request, pageParamRequest, systemAdmin)));
    }

    @ApiOperation(value = "结算记录详情")
    @RequestMapping(value = "/closing/record/detail/{closingNo}", method = RequestMethod.GET)
    public CommonResult<MerchantClosingDetailResponse> getMerchantClosingDetail(@PathVariable(value = "closingNo") String closingNo) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(financeService.getMerchantClosingDetailByMerchant(closingNo, systemAdmin));
    }

    @ApiOperation(value = "日帐单管理分页列表")
    @RequestMapping(value = "/daily/statement/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MerchantStatementResponse>> getDailyStatementList(@RequestParam(value = "dateLimit", required = false, defaultValue = "") String dateLimit,
                                                                                     @ModelAttribute PageParamRequest pageParamRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(CommonPage.restPage(financeService.getMerchantDailyStatementList(dateLimit, pageParamRequest, systemAdmin)));
    }

    @ApiOperation(value = "月帐单管理分页列表")
    @RequestMapping(value = "/month/statement/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MerchantStatementResponse>> getMonthStatementList(@RequestParam(value = "dateLimit", required = false, defaultValue = "") String dateLimit,
                                                                                     @ModelAttribute PageParamRequest pageParamRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(CommonPage.restPage(financeService.getMerchantMonthStatementList(dateLimit, pageParamRequest, systemAdmin)));
    }
}



